package com.ipay.redis;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

/**
 * 
 * @author yangk
 *
 */
@Component
public class JedisConnectionPool {
	
	private static Log log = LogFactory.getLog(JedisConnectionPool.class);

	@Value("${jedis.pool.maxActive}") private int maxActive = 20;
	@Value("${jedis.pool.maxIdle}") private int maxIdle = 10;
	@Value("${jedis.pool.minIdle}") private int minIdle = 5;
	@Value("${jedis.pool.minIdleTime}") private int minIdleTime = 300 * 1000;
	@Value("${jedis.pool.softMinIdleTime}") private int softMinIdleTime = -1;
	@Value("${jedis.pool.maxWait}") private int maxWait = 1000;
	@Value("${jedis.pool.testOnBorrow}") private boolean testOnBorrow = false;
	@Value("${jedis.pool.host}") private String host = "39.108.174.184";
	@Value("${jedis.pool.port}") private int port = 6379;
	@Value("${jedis.pool.enable}") private boolean enable = true;

	///微信端登录地址
	@Value("${wechat.baseurl}")
	private String baseUrl;
	@Value("${wechat.baseurl.http}")
	private String baseUrlHttp;
	//后台登录地址
	@Value("${domainurl.manager.http}")
	private String domainurlManagerHttp;

	//#图片上传路径
	@Value("${upload.img.path}")
	private String uploadImgPath;
	
	public int getMaxActive() {
		return maxActive;
	}

	public void setMaxActive(int maxActive) {
		this.maxActive = maxActive;
	}

	public int getMaxIdle() {
		return maxIdle;
	}

	public void setMaxIdle(int maxIdle) {
		this.maxIdle = maxIdle;
	}

	public int getMinIdle() {
		return minIdle;
	}

	public void setMinIdle(int minIdle) {
		this.minIdle = minIdle;
	}

	public int getMinIdleTime() {
		return minIdleTime;
	}

	public void setMinIdleTime(int minIdleTime) {
		this.minIdleTime = minIdleTime;
	}

	public int getSoftMinIdleTime() {
		return softMinIdleTime;
	}

	public void setSoftMinIdleTime(int softMinIdleTime) {
		this.softMinIdleTime = softMinIdleTime;
	}

	public int getMaxWait() {
		return maxWait;
	}

	public void setMaxWait(int maxWait) {
		this.maxWait = maxWait;
	}

	public boolean isTestOnBorrow() {
		return testOnBorrow;
	}

	public void setTestOnBorrow(boolean testOnBorrow) {
		this.testOnBorrow = testOnBorrow;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public ShardedJedisPool getShardPool() {
		return shardPool;
	}

	public void setShardPool(ShardedJedisPool shardPool) {
		this.shardPool = shardPool;
	}

	public ShardedJedisPool shardPool;//支持集群

	public String getBaseUrl() {
		return baseUrl;
	}

	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	public String getBaseUrlHttp() {
		return baseUrlHttp;
	}

	public void setBaseUrlHttp(String baseUrlHttp) {
		this.baseUrlHttp = baseUrlHttp;
	}

	public String getUploadImgPath() {
		return uploadImgPath;
	}

	public void setUploadImgPath(String uploadImgPath) {
		this.uploadImgPath = uploadImgPath;
	}

	public String getDomainurlManagerHttp() {
		return domainurlManagerHttp;
	}

	public void setDomainurlManagerHttp(String domainurlManagerHttp) {
		this.domainurlManagerHttp = domainurlManagerHttp;
	}

	@PostConstruct
	private void init(){
		if(!enable){ return;}
		log.info("Jedis init....");
		log.info("Jedis pool info : ip=" + host + "  port=" + port);
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(maxIdle);
		config.setMinIdle(minIdle);
		config.setMinEvictableIdleTimeMillis(minIdleTime);
		if(softMinIdleTime > 0){config.setSoftMinEvictableIdleTimeMillis(softMinIdleTime);}
		config.setTestOnBorrow(testOnBorrow);
		List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
		shards.add(new JedisShardInfo(host, port));
		this.shardPool = new ShardedJedisPool(config, shards);
		log.info("Jedis init success");
	}
	
	public void destroy(){
		this.shardPool.destroy();
	}
	
}